/***********************
*DESC:完成xml数据至json数据的转换，生成本地cfgJson数据，生成默认配置参数类型map
*Author:wff 
*Date:2015-04-27
*
************************/
var confTypeMap = new Map();
var ctrlTypeMap = new Map();
var interfaceTypeMap = new Map();
var LONinterfaceTypeMap = new Map();
var l880interfaceTypeMap = new Map();
var NICODMX512interfaceTypeMap = new Map();
var l830linterfaceTypeMap = new Map();
var areaTypeMap = new Map();
var elemTypeMap = new Map();
var cmdTypeMap = new Map();
var ModelTypeMap = new Map();
var sceneTypeMap = new Map();
var floorTypeMap = new Map();
var cmdOemIdModelMap = new Map();
var xotree = new XML.ObjTree();
var dumper = new JKL.Dumper();
var xmlText;
var tree;
var ViewCfgtree;
var cfgtree;
var paramTypejsonData;
var cfgjsonData;
var ViewCfgjsonData;
var LogicCfgjsonData;
var LONinterfaceTypeRangeMap = new Map();
var Initl880interfaceArray = [];
var Initl830linterfaceArray = [];
var InitNICODMX512interfaceArray = [];
var shapeTypeMap = new Map();
var dirTypeMap = new Map();

 function CreatparamTypMap(json)
{
confTypeMap.put('01','协议型');
confTypeMap.put('02','配置型');
/*彩灯*/
shapeTypeMap.put('1','直线');
shapeTypeMap.put('2','圆');
shapeTypeMap.put('3','矩形');
dirTypeMap.put('0','请选择');
dirTypeMap.put('1','顺时针');
dirTypeMap.put('2','逆时针');

$.each(json.paramTypes.paramType,function(index,item){
		
		
		switch(item.type){
			case 'ctrlType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						ctrlTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'interfaceType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						interfaceTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'LONinterfaceType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						LONinterfaceTypeMap.put(Subitem.code,Subitem.name);
						LONinterfaceTypeRangeMap.put(Subitem.code,Subitem.range);
				});
				break;
			case 'l880interfaceType':
				Initl880interfaceArray = json.paramTypes.paramType[index];
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						l880interfaceTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'NICODMX512interfaceType':
				InitNICODMX512interfaceArray = json.paramTypes.paramType[index];
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						NICODMX512interfaceTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'l830linterfaceType':
				Initl830linterfaceArray = json.paramTypes.paramType[index];
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						l830linterfaceTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'floorType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						floorTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'areaType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						areaTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'elemType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						elemTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			case 'sceneType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						sceneTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;
			/*case 'modelType':
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						ModelTypeMap.put(Subitem.code,Subitem.name);								  
				});
				break;*/				
			case 'cmdType':
				InitMcmdArry = json.paramTypes.paramType[index];
				$.each(json.paramTypes.paramType[index].param,function(Subindex,Subitem){
						cmdTypeMap.put(Subitem.ctype,Subitem.name);								  
				});
				break;
			default:
				break;
		}
	})
	
}
var paramTypemap = new Map();
function FillSelectSetCurVal(paramTypemap,selectId,CurVal)
{
	
	$("#" + selectId).empty();
		var selectedValue;
		var length = paramTypemap.size();
		
		for ( var i = 0; i < length; i++) {
			if(i == 0)
				selectedValue = paramTypemap.getKey(i);
				
			$("#" + selectId).append("<option value="+paramTypemap.getKey(i)+">"+paramTypemap.getValue(i)+"</option>"); 
	}
	$("#" + selectId).val(CurVal);
	$("#" + selectId + " option[value='" + CurVal + "']").attr("selected","selected");
}
function FillSelect(paramTypemap,selectId)
{
	
	$("#" + selectId).empty();
		var selectedValue;
		var length = paramTypemap.size();
		
		for ( var i = 0; i < length; i++) {
			if(i == 0)
				selectedValue = paramTypemap.getKey(i);
				
			$("#" + selectId).append("<option value="+paramTypemap.getKey(i)+">"+paramTypemap.getValue(i)+"</option>"); 
	}
	$("#" + selectId).val(selectedValue);
	$("#" + selectId + " option[value='" + selectedValue + "']").attr("selected","selected");
}
function FillNoChoosedSelect(paramTypemap,selectId)
{
	
	$("#" + selectId).empty();
		
		var length = paramTypemap.size();
		
		for ( var i = 0; i < length; i++) {
							
			$("#" + selectId).append("<option value="+paramTypemap.getKey(i)+">"+paramTypemap.getValue(i)+"</option>"); 
	}
	
}

/*获取paramTypexml 转换成json*/
function GetparamTypexmlTjson(){
$.ajax({                 
				 
				url : '../orgMgr/devCfg/loadCfgTypes',				
                type: 'get',  
                dataType: 'text',  
                timeout: 100000,  
                cache: false,
				                
				error : function() {
					alert("获取参数超时");				
				},    
                success:  function(dataxml) {
				
					tree = xotree.parseXML(dataxml);
					
					paramTypejsonData = dumper.dump(tree);
					paramTypejsonData = $.parseJSON(paramTypejsonData);
					CreatparamTypMap(paramTypejsonData);
					initSelect(currusername);
					GetDevModelxmlTjson();
					GetAppCfgxmlTjson();
					 
				}
				   
})
}
var CurDevModelElemTypeArray = [];
var curmodelarray = [];
var xotreemodel = new XML.ObjTree();
var GetCurrModelObj;
var dumperdev = new JKL.Dumper();
var isModelCfgNull = false;
/*获取DevModelxml 转换成json*/
function GetDevModelxmlTjson(){
$.ajax({                 
				 
				url : '../adminMgr/device/loadDevModelCfg',				
                type: 'get',  
                dataType: 'text',  
                timeout: 100000,  
                cache: false,
				data : {orgId : parent.CurrorgID},               
				error : function() {
					alert("获取参数超时");				
				},    
                success:  function(dataxml) {
				
					if (dataxml == "") {
						isModelCfgNull = true;
					} else {
						
						var tree = xotreemodel.parseXML(dataxml);
						GetCurrModelObj = $.parseJSON(dumperdev.dump(tree));
		
						
						if (ParseModelCfgJSONData() == true) {
							//console.log("初始化解析后的CurDevModelElemTypeArray:", CurDevModelElemTypeArray);
							var etype=$("#etype").children("option:selected").val();
							
							var CurModelArray=GetCurModelArrayData(etype);
							//console.log("初始化解析后的CurModelArray:", CurModelArray);
							var CurCmdArray=GetCurCmdArrayData(CurModelArray);
							//console.log("初始化解析后的CurCmdArray:", CurCmdArray);//etype
							FillSelectCmdModel(CurCmdArray,'modelselect');
						}
						
					}
					 
				}
				   
})
}
/****xml数据解析****/
function ParseModelCfgJSONData() {
	var curmodelarray = [];
	if (!GetCurrModelObj.paramTypes.hasOwnProperty("elemType")) {
		return true;
	}
	CurDevModelElemTypeArray = [];
	var newelemType = GetCurrModelObj.paramTypes.elemType;
	if (typeof(newelemType) == "object" && newelemType.constructor == Array) { //多个elemType
		curmodelarray = [];
		$.each(newelemType, function (indexnewelemType, itemnewelemType) {
			var elemtype = itemnewelemType.etype;
			var elemname = itemnewelemType.name;
			//CurDevModelElemTypeArray.push({"etype":elemtype,"name":elemname});
			if (itemnewelemType.hasOwnProperty("model")) {
				var newmodel = itemnewelemType.model;
				if (typeof(newmodel) == "object" && newmodel.constructor == Array) { //多个model
					curmodelarray = []; ////////////////jia
					$.each(newmodel, function (indexnewmodel, itemnewmodel) {
						var mid = itemnewmodel.id;
						var modelmodel = itemnewmodel.model;
						var ischeckAddr1 = itemnewmodel.ischeckAddr1;
						var ischeckAddr2 = itemnewmodel.ischeckAddr2;
						var addr1Name = itemnewmodel.addr1Name;
						var addr2Name = itemnewmodel.addr2Name;
						if (itemnewmodel.hasOwnProperty("cmd")) {
							var newcmd = itemnewmodel.cmd;
							curmodelarray.push({
								"id" : mid,
								"model" : modelmodel,
								"ischeckAddr1" : ischeckAddr1,
								"ischeckAddr2" : ischeckAddr2,
								"addr1Name" : addr1Name,
								"addr2Name" : addr2Name,
								"cmd" : newcmd
							}); ////////jia
							//return true;
						} else { //无cmd
							curmodelarray.push({
								"id" : mid,
								"model" : modelmodel,
								"ischeckAddr1" : ischeckAddr1,
								"ischeckAddr2" : ischeckAddr2,
								"addr1Name" : addr1Name,
								"addr2Name" : addr2Name
							}); ////////jia
							//return true;
						}
					});
				} else { //一个model
					//newmodel
					curmodelarray = [];
					if (newmodel.hasOwnProperty("cmd")) { //有cmd
						var newcmd = newmodel.cmd;
						curmodelarray.push({
							"id" : newmodel.id,
							"model" : newmodel.model,
							"ischeckAddr1" : newmodel.ischeckAddr1,
							"ischeckAddr2" : newmodel.ischeckAddr2,
							"addr1Name" : newmodel.addr1Name,
							"addr2Name" : newmodel.addr2Name,
							"cmd" : newcmd
						}); ////////jia
						//return true;
					} else { //无cmd
						curmodelarray.push({
							"id" : newmodel.id,
							"model" : newmodel.model,
							"ischeckAddr1" : newmodel.ischeckAddr1,
							"ischeckAddr2" : newmodel.ischeckAddr2,
							"addr1Name" : newmodel.addr1Name,
							"addr2Name" : newmodel.addr2Name
						}); ////////jia
						//return true;
					}
				}
			}
			CurDevModelElemTypeArray.push({
				"etype" : elemtype,
				"name" : elemname,
				"model" : curmodelarray
			});
			return true;
		});
		return true;
	} else { //一个elemType
		//newelemType
		var elemtype = newelemType.etype;
		var elemname = newelemType.name;
		if (newelemType.hasOwnProperty("model")) {
			var newmodel = newelemType.model;
			if (typeof(newmodel) == "object" && newmodel.constructor == Array) { //多个model
				curmodelarray = [];
				$.each(newmodel, function (indexnewmodel, itemnewmodel) {
					var mid = itemnewmodel.id;
					var modelmodel = itemnewmodel.model;
					//var ischeckAddr1 = itemnewmodel.ischeckAddr1;
					//var ischeckAddr2 = itemnewmodel.ischeckAddr2;
					var addr1Name = itemnewmodel.addr1Name;
					var addr2Name = itemnewmodel.addr2Name;
					if (itemnewmodel.hasOwnProperty("cmd")) { //has cmd
						var newcmd = itemnewmodel.cmd;
						curmodelarray.push({
							"id" : mid,
							"model" : modelmodel,
							//"ischeckAddr1" : ischeckAddr1,
							//"ischeckAddr2" : ischeckAddr2,
							"addr1Name" : addr1Name,
							"addr2Name" : addr2Name,
							"cmd" : newcmd
						});
					} else { //无cmd
						curmodelarray.push({
							"id" : mid,
							"model" : modelmodel,
							//"ischeckAddr1" : ischeckAddr1,
							//"ischeckAddr2" : ischeckAddr2,
							"addr1Name" : addr1Name,
							"addr2Name" : addr2Name
						});
					}
				});
			} else { //一个model
				//newmodel
				curmodelarray = [];
				if (newmodel.hasOwnProperty("cmd")) { //有cmd
					var newcmd = newmodel.cmd;
					curmodelarray.push({
						"id" : newmodel.id,
						"model" : newmodel.model,
						//"ischeckAddr1" : newmodel.ischeckAddr1,
						//"ischeckAddr2" : newmodel.ischeckAddr2,
						"addr1Name" : newmodel.addr1Name,
						"addr2Name" : newmodel.addr2Name,
						"cmd" : newcmd
					});
					//return true;
				} else { //无cmd
					curmodelarray.push({
						"id" : newmodel.id,
						"model" : newmodel.model,
						//"ischeckAddr1" : newmodel.ischeckAddr1,
						//"ischeckAddr2" : newmodel.ischeckAddr2,
						"addr1Name" : newmodel.addr1Name,
						"addr2Name" : newmodel.addr2Name
					});
				}
			}
		}
		CurDevModelElemTypeArray.push({
			"etype" : elemtype,
			"name" : elemname,
			"model" : curmodelarray
		});
		return true;
	}
	return true;
}
/****根据设备类型获取CurModelArray****/
function GetCurModelArrayData(etype)
{
	var data = [];
	$.each(CurDevModelElemTypeArray, function (index, item) {
		if (item.etype == etype) {
			if (item.hasOwnProperty("model")) {
				data = item.model;
			}
		}
	});
	return $.makeArray(data);
}

/****获取CurCmdArray****/
function GetCurCmdArrayData(CurModelArray) 
{
	var CurCmdArray = [];
	$.each(CurModelArray, function (index, item) {
		
			if (item.hasOwnProperty("cmd")) {
				var newcmd=item.cmd;
				newcmd=$.makeArray(newcmd);
				$.each(newcmd, function (indexcmd, itemcmd) {
					CurCmdArray.push({"mid":item.id,"model":item.model,"addr1Name":item.addr1Name,"addr2Name":item.addr2Name,"cid":itemcmd.id,"ctype":itemcmd.ctype,"rw":itemcmd.rw,"modelName":itemcmd.modelName,"i":itemcmd.i});
				});
			}
		
	});
	return $.makeArray(CurCmdArray);
}
function FillSelectCmdModel(paramTypemap,selectId)
{
	
	$("#" + selectId).empty();
		var selectedValue;
		var length = paramTypemap.length;
		
		for ( var i = 0; i < length; i++) {
			if(i == 0)
				selectedValue = paramTypemap[i].cid;
				
			$("#" + selectId).append("<option data-addr1Name='"+paramTypemap[i].addr1Name+"' data-addr2Name='"+paramTypemap[i].addr2Name+"' data-model='"+paramTypemap[i].model+"' value='"+paramTypemap[i].cid+"'>"+paramTypemap[i].modelName+"</option>"); 
	}
	$("#" + selectId).val(selectedValue);
	$("#" + selectId + " option[value='" + selectedValue + "']").attr("selected","selected");
}
function GetViewcfgparamxmlTjson(currusername){
	
	$.ajax({                 
				 
				url : '../orgMgr/devCfg/loadCfgTypes',				
                type: 'get',  
                dataType: 'text',  
                timeout: 100000,  
                cache: false,
				data:{devId: currusername},                
				error : function() {
					alert("获取参数超时");				
				},    
                success:  function(cfgxml) {
				
					/*ViewCfgtree = xotree.parseXML(cfgxml);				
					ViewCfgjsonData = dumper.dump(ViewCfgtree);					
					ViewCfgjsonData = $.parseJSON(ViewCfgjsonData);
					
					initAreaType(currusername,currfloorid);	
					$('#myTab a:first').tab('show');*/
				}
				   
})
}



